package com.julis.activity.setting.updata;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.julis.R;
import com.julis.html_analyse.HtmlResultBean;
import com.julis.html_analyse.UpdateHelper;
import com.julis.init.PackageUtils;

/**
 * 远程升级
 */
public class Setting_Remoteupgrade_Activity extends AppCompatActivity {


    @BindView(R.id.imageView15)
    ImageView imageView15;
    @BindView(R.id.textView11)
    TextView textView11;
    @BindView(R.id.layout_cjwt)
    RelativeLayout layoutCjwt;
    @BindView(R.id.textView12)
    TextView textView12;
    @BindView(R.id.imageView16)
    ImageView imageView16;
    @BindView(R.id.layout_ywgn)
    RelativeLayout layoutYwgn;
    @BindView(R.id.tv_bb)
    TextView tvBb;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.layout_version)
    RelativeLayout layoutVersion;
    @BindView(R.id.activity_seetting)
    LinearLayout activitySeetting;
    @BindView(R.id.textView11z)
    TextView textView11z;

    UpdateHelper updateHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remoteupgrade);
        ButterKnife.bind(this);
        updateHelper=new UpdateHelper();
        textView11z.setText("当前版本：" + PackageUtils.getVersionName(Setting_Remoteupgrade_Activity.this));
    }

    @OnClick({R.id.img_back, R.id.layout_ywgn, R.id.layout_cjwt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.layout_cjwt:
                startActivity(new Intent(Setting_Remoteupgrade_Activity.this, Setting_IP_Activity.class));
                break;
            case R.id.layout_ywgn:
                new Thread() {
                    public void run() {
                        HtmlResultBean updateInfo = updateHelper.getUpdateInfo(Setting_Remoteupgrade_Activity.this);
                        if (updateInfo.getCode() != 0) {
                            handler.obtainMessage(MSG_TYPE_GET_UPDATE_INFO_FAILURE, updateInfo).sendToTarget();
                        }
                    }


                }.start();
                break;
        }
    }

    private final int MSG_TYPE_GET_UPDATE_INFO_FAILURE = 0;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_TYPE_GET_UPDATE_INFO_FAILURE:
                    HtmlResultBean htmlResultBean = (HtmlResultBean) msg.obj;
                    Toast.makeText(Setting_Remoteupgrade_Activity.this, htmlResultBean.getMsg(), Toast.LENGTH_SHORT).show();
                    break;

                default:
                    break;
            }
        }


    };


}
